package com.zx.wangyixinwen.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.activity.WomanAcitivy;
import com.zx.wangyixinwen.adapter.WomanAdapter;
import com.zx.wangyixinwen.bean.Woman;
import com.zx.wangyixinwen.bean.WomanBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/6.
 */
public class WomanFragment extends Fragment {
    private ListView listView;
    private RequestQueue queue;
    private List<Woman> datas;
    private WomanAdapter adapter;
    private Context context;
    private String url = "http://zzd.sm.cn/appservice/api/v1/channel/1404457531633?client_os=android&client_version=2.1.0.1&bid=800&m_ch=006&city=0411&sn=10d5c82630e5931bf2562db691499e0f&content_cnt=20&recoid=103163883" +
            "6323992873&ftime=1453211846787&method=new&count=20";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_woman, null);
        listView = (ListView) view.findViewById(R.id.listView_woman);
        addData();
        return view;
    }

    private void addData() {
        queue = Volley.newRequestQueue(getActivity());
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                datas = new ArrayList<>();
                final Woman woman = gson.fromJson(response, Woman.class);
                datas.add(woman);
                for (int i = 0; i < datas.get(0).getData().getArticle().get(i).getImage().size(); i++) {
                    WomanBean womanBean = new WomanBean();
                    womanBean.setPricure(datas.get(0).getData().getArticle().get(i).getImage().get(0).getUrl());
                    womanBean.setTitle(datas.get(0).getData().getArticle().get(i).getTitle());
                    womanBean.setImageSize(datas.get(0).getData().getArticle().get(i).getImage().size());
                    for (int j = 0; j < datas.get(0).getData().getArticle().get(i).getImage().size(); j++) {
                        womanBean.setImageSrcs(datas.get(0).getData().getArticle().get(i).getImage().get(j).getUrl());
                    }
                }

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getActivity(), WomanAcitivy.class);
                        intent.putExtra("pos", position);
                        intent.putExtra("datas", (Serializable) datas);
                        startActivity(intent);
                    }
                });
                adapter = new WomanAdapter(datas,context);
                listView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(stringRequest);
    }

}
